import 'package:flutter/material.dart';
import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
import 'package:hbtrans_flutter/utils/fluro.dart';

class MyIjkPlayer extends StatefulWidget {
  final url;

  const MyIjkPlayer({Key key, this.url}) : super(key: key);

  @override
  _MyIjkPlayerState createState() => _MyIjkPlayerState();
}

class _MyIjkPlayerState extends State<MyIjkPlayer> {
  var controller = IjkMediaController();

  Orientation get orientation => MediaQuery.of(context).orientation;

  @override
  void initState() {
    super.initState();

    var urls = FluroConvertUtils.fluroCnParamsDecode(widget.url).split('/');
    String s = Uri.encodeComponent('${urls[5]}/${urls[6]}');
    DataSource source =
        DataSource.network('${urls[0]}//${urls[2]}/${urls[3]}/${urls[4]}/$s');
    controller.setDataSource(source, autoPlay: true);
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (orientation == Orientation.landscape) {
      return _buildMyIjkPlayerPlayer();
    }
    return Scaffold(
      appBar: AppBar(
        title: Text("视频播放"),
      ),
      body: ListView(
        children: <Widget>[
          _buildPlayerItem(),
        ],
      ),
    );
  }

  _buildPlayerItem() {
    return Container(
      height: 200,
      child: IjkPlayer(
        mediaController: controller,
      ),
    );
  }

  _buildMyIjkPlayerPlayer() {
    var data = MediaQuery.of(context);
    return Material(
      child: Container(
        width: data.size.width,
        height: data.size.height,
        child: IjkPlayer(
          mediaController: controller,
        ),
      ),
    );
  }
}
